<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:a="https://github.com/pylonide/pylon" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>TEST BOT</title>
        <script type="text/javascript" src="../ppc.js"></script>
    </head>
    <body>
        <a:skin src="../skins.xml" />
            
        <a:appsettings name   = "worknets" debug="false"
          disable-right-click = "false"
          disable-space       = "true"
          disable-f5          = "false"
          auto-hide-loading   = "true"
          allow-select        = "true"
          allow-blur          = "true"
          initdelay           = "false"
          loading-message     = "Loading..."
          empty-message       = "Loading..."
          baseurl             = "{ppc.host ? ppc.host + '/' : 'http://dev.worknets.com/'}">
        </a:appsettings>

        <a:xmpp id="myXMPP"
            url        = "{ppc.host ? ppc.host : 'http://localhost'}:5280/http-bind"
            host       = "worknets.com"
            rdb-bot    = "true"
            type       = "binding"
            resource   = "{'ppc_'.appendRandomNumber(5)}" />    
    
        <a:remote transport="myXMPP" id="rmtFs" />
    
        <a:script><![CDATA[
            self.getPrio = function() {
                return 42;
            };
            
    		function createBot(user, pass, prio){
    			var model;

    	        var cMyXmpp = myXMPP.cloneNode(true);
    			cMyXmpp.setAttribute("id", "cMyXmpp" + prio);
    			cMyXmpp.setAttribute("priority", prio);
    			ppc.document.body.appendChild(cMyXmpp);
    			var cRmtFs  = rmtFs.cloneNode(true);
    			cRmtFs.setAttribute("transport", "cMyXmpp" + prio);
    			ppc.document.body.appendChild(cRmtFs);
    
    			rmtFs.addEventListener("change", function(e) {
    				ppc.console.log("### INCOMING change: " + e.resource + "\n");
    				//persist.updateModel(e.resource, e.model);
    			});
    			rmtFs.addEventListener("rdbinit", function(e) {
    				ppc.console.log("### RDBINIT called: " + e.resource + ", " + e.model);
    				e.model.load("<data><caption>" + user + ": 1</caption></data>");
    				model = e.model;
    			});
    
    			var i=2;
    			setTimeout(function() {
    				try {
    					console.log("update");
    					model && model.setQueryValue("/data/caption", user + ": " + i++);
    				} catch (e) {
    					console.error(e);
    				} finally {
    					setTimeout(arguments.callee, 3000);
    				}
    			}, 1000);
    
    			cMyXmpp.connect(user, pass);
    			
    			return {
    				xmpp: cMyXmpp,
    				remote: cRmtFs,
    				name: user
    			}
    		}
    		
    		createBot("bot1", "botz1", 41);
            createBot("bot2", "botz2", 42);       
        ]]></a:script>

    </body>
</html>
